<?php
abstract class WebBase extends Wherein_knowledgeModuleSite{
    public $modulename;
    public $module;
    public $weid;
    public $uniacid;
    public $__define;

    public function __construct() {
    }

    public function __set($name,$value){
        $this->$name = $value;
    }


    //输出模板
    protected function template($filename) {
        global $_W,$_GPC;

        $name = strtolower($this->modulename);
        $defineDir = dirname($this->__define);

        $compile = IA_ROOT . "/data/tpl/app/{$_W['template']}/{$name}/".$_GPC['do']."/{$filename}.tpl.php";

        $_filename = explode('/',$filename);
        if(count($_filename)>1){
            $source = $defineDir . "/application/app/view/{$filename}.html";
        }else{
            $source = $defineDir . "/application/app/view/".$_GPC['do']."/{$filename}.html";
        }
        if(!is_file($source)) {
            $source = IA_ROOT . "/app/themes/default/{$filename}.html";
            $compile = IA_ROOT . "/data/tpl/app/default/{$filename}.tpl.php";
        }
        if(!is_file($source)) {
            exit("Error: template source '{$filename}' is not exist!");
        }
        $paths = pathinfo($compile);
        $compile = str_replace($paths['filename'], $_W['uniacid'] . '_' . $paths['filename'], $compile);
        if (DEVELOPMENT || !is_file($compile) || filemtime($source) > filemtime($compile)) {
            template_compile($source, $compile, true);
        }
        return $compile;
    }

    //消息提示
    public function message_new($msg,$redirect,$type){
        global $_W;
        if($redirect == 'refresh') {
            $redirect = $_W['script_name'] . '?' . $_SERVER['QUERY_STRING'];
        } elseif (!empty($redirect) && !strexists($redirect, 'http://')) {
            $urls = parse_url($redirect);
            $redirect = $_W['siteroot'] . 'app/index.php?' . $urls['query'];
        }
        if($redirect == '') {
            $type = in_array($type, array('success', 'error', 'info', 'warning', 'ajax', 'sql')) ? $type : 'info';
        } else {
            $type = in_array($type, array('success', 'error', 'info', 'warning', 'ajax', 'sql')) ? $type : 'success';
        }
        if($_W['isajax'] || $type == 'ajax') {
            $vars = array();
            $vars['message'] = $msg;
            $vars['redirect'] = $redirect;
            $vars['type'] = $type;
            exit(json_encode($vars));
        }
        if (empty($msg) && !empty($redirect)) {
            header('location: '.$redirect);
        }
        $label = $type;
        if($type == 'error') {
            $label = 'danger';
        }
        if($type == 'ajax' || $type == 'sql') {
            $label = 'warning';
        }
        if (defined('IN_API')) {
            exit($msg);
        }
        include $this->template('message/message');
        exit();
    }

}